package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 供应商
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    /**
     * 删除供应商（支持批量删除）
     */
    @PostMapping("/delete")
    public ServiceVO deleteSupplierById(@RequestParam("ids") String ids){
        supplierService.deleteSupplierById(ids);
        ServiceVO serviceVO = new ServiceVO(100,"请求成功",null);
        return serviceVO;
    }

    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */
    @PostMapping("/list")
    public Map<String,Object> list(Integer page,
                    Integer rows,
                    String supplierName){
//        List<Supplier> list = supplierService.list(supplierName);
//        Map<String,Object> map = new HashMap<>();
//        map.put("total",list.size());
//        map.put("rows",list);
//
//        return map;
        PageHelper.startPage(page, rows);
        List<Supplier> supplierList = supplierService.list(supplierName);
        PageInfo<Supplier> pageInfo = new PageInfo<>(supplierList);
        Map<String,Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", pageInfo.getList());
        return map;
    }

    /**
     * 供应商添加或修改
     * @param supplierName
     * @param contacts
     * @param phoneNumber
     * @param address
     * @param remarks
     * @return
     */
    @PostMapping("/save")
    public ServiceVO save(@Param("supplierId") Integer supplierId,
                          @RequestParam("supplierName") String supplierName,
                          @RequestParam("contacts") String contacts,
                          @RequestParam("phoneNumber") String phoneNumber,
                          @RequestParam("address") String address,
                          @RequestParam("remarks") String remarks
                          ){

        Supplier supplier = new Supplier();
        supplier.setSupplierId(supplierId);
        supplier.setSupplierName(supplierName);
        supplier.setContacts(contacts);
        supplier.setAddress(address);
        supplier.setPhoneNumber(phoneNumber);
        supplier.setRemarks(remarks);
        supplierService.save(supplier);
        //supplierService.save(supplierName,contacts,phoneNumber,address,remarks);
        ServiceVO serviceVO = new ServiceVO(100,"请求成功",null);
        return serviceVO;
    }
}
